/*
** Copyright (C) 2009 Joshua Brent Marsh <joshua@icub3d.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

/* $Id: test_string.c 45 2009-11-17 02:12:27Z joshua@icub3d.com $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <malloc.h>

#include <CUnit/Basic.h>

#include "../src/istring.h"
#include "../src/function_prototypes.h"
#include "../src/list.h"

#include "test_string.h"

int
init_test_string_suite ()
{
  return 0;
}

int
clean_test_string_suite ()
{
  return 0;
}


void
test_string_strmcats ()
{
  char **cur;
  char *sum;
  char * strings[] = {
    "This is a test ",
    "of the emergency ",
    "broadcast system. ",
    "Please don't panic!",
    NULL
  };

  cur = strings;

  sum = NULL;

  /* Cat our string list together. */
  while (*cur != NULL)
    {
      if (strmcat (&sum, *cur) == 0)
	CU_FAIL ("strmcat returned 0.");

      cur++;
    }

  /* Make sure it looks like what we expected. */
  CU_ASSERT_STRING_EQUAL (sum, "This is a test of the emergency broadcast system. Please don't panic!");

  free (sum);
}

void
test_string_strltok ()
{
  List *l;

  char *csv = "This,Is,A,\"test,test\",\"of\"";
  char *tab = "This\tIs\t\"A\"\ttest,test\tof\t\t";
  char *fail = "This,Is,A,\"test,test,of";

  /* Check for a fail case. */
  l = strltok (fail, ",", "\"", "");
  CU_ASSERT (l == NULL);
  
  /* Check for a CSV type. */
  l = strltok (csv, ",", "\"", "");
  CU_ASSERT (l != NULL);
  CU_ASSERT_STRING_EQUAL (list_get (l, 0), "This");
  CU_ASSERT_STRING_EQUAL (list_get (l, 1), "Is");
  CU_ASSERT_STRING_EQUAL (list_get (l, 2), "A");
  CU_ASSERT_STRING_EQUAL (list_get (l, 3), "test,test");
  CU_ASSERT_STRING_EQUAL (list_get (l, 4), "of");
  list_destroy (l, &dest_string);

  /* Check for a tab type. */
  l = strltok (tab, "\t", "\"", "");
  CU_ASSERT (l != NULL);
  CU_ASSERT_STRING_EQUAL (list_get (l, 0), "This");
  CU_ASSERT_STRING_EQUAL (list_get (l, 1), "Is");
  CU_ASSERT_STRING_EQUAL (list_get (l, 2), "A");
  CU_ASSERT_STRING_EQUAL (list_get (l, 3), "test,test");
  CU_ASSERT_STRING_EQUAL (list_get (l, 4), "of");
  CU_ASSERT_STRING_EQUAL (list_get (l, 5), "");
  CU_ASSERT_STRING_EQUAL (list_get (l, 6), "");
  list_destroy (l, &dest_string);
  
}

void
test_string_strdcats ()
{
  char *str;

  /* Test unallocated. */
  str = NULL;
  CU_ASSERT (strdcat (&str, " ", 4, "This", "is", "a", "test.") == 1);
  CU_ASSERT (strcmp (str, "This is a test.") == 0);
  free (str);

  /* Test unallocated, but limited. Has same effect as preallocated. */
  str = NULL;
  CU_ASSERT (strdncat (&str, ">|<", 12, 4, "This", "is", "a", "test.") == 1);
  CU_ASSERT (strcmp (str, "This>|<is>|<") == 0);
  free (str);

}

int
make_string_suite (CU_pSuite s)
{
  s = CU_add_suite ("Test String Suite", 
		    init_test_string_suite, 
		    clean_test_string_suite);
  if (s == NULL)
    return CU_get_error();

  if (CU_add_test (s, "test string_strmcats", 
		   test_string_strmcats) == NULL ||
      CU_add_test (s, "test string_strltok", 
		   test_string_strltok) == NULL ||
      CU_add_test (s, "test string_strdcats", 
		   test_string_strdcats) == NULL)
    {
      return CU_get_error();
    }

  return CU_get_error();

}

